/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/
package com.nurdrobotics;

import edu.wpi.first.wpilibj.Joystick;

/**
 * 
 */
public class ShooterStick {
    // The following variables gives a name to the port that is connected
    // to the shooter joystick.
    // This way, it's easy to look at this section of code to make sure that
    // the shooter joystick is configured correctly in the DriverStation

    private static final int PORT_SHOOTER = 2;
    // The following variables define names for the axes on the shooter
    // joystick
    private static final int AXIS_PITCH = 2;
    private static final int AXIS_YAW = 1;
    // The follwoing variables define names for all the buttons on the
    // shooter joystick
    private static final int BUTTON_SHOOT = 1;
    private static final int BUTTON_REVERSE = 8;
    private static final int BUTTON_FULL_AUTO = 7;
    private static final int BUTTON_DEPLOY_WEDGE = 11;
    private static final int BUTTON_RETRACT_WEDGE = 10;
    // declare an variable for holding the joystick
    private static Joystick m_joystick = null;
    // create boolean values to remember the last state of buttons
    // for rising edges
    private static boolean shootLastPressedRising = false;
    private static boolean reverseLastPressedRising = false;
    /*
     * create limit switch instances for pitch up and down
     */
    private static LimitSwitch pitchUpLimit = new LimitSwitch(LimitSwitch.PORT_PITCH_UP);
    private static LimitSwitch pitchDownLimit = new LimitSwitch(LimitSwitch.PORT_PITCH_DOWN);

    /*
     * This function initializes variables in this class. It should
     * be called exactly once prior to using the class.
     */
    public static boolean init() {
        // initialize the joystick
        m_joystick = new Joystick(PORT_SHOOTER);
        if (m_joystick == null) {
            return false;
        }
        return true;
    }

    /*
     * Returns true if the deploy wedge button is pressed, false otherwise
     */
    public static boolean shootPressed() {
        return (m_joystick.getRawButton(BUTTON_SHOOT));
    }

    /*
     * This routine returns true if the shoot button is pressed and wasn't
     * pressed the prior time this routine was called.
     */
    public static boolean shootRisingEdge() {
        // determine whether the button is currently pressed
        boolean pressed = shootPressed();

        // by default, set the return value to reflect this is not a rising edge
        boolean returnValue = false;

        // if the button is currently pressed, and it wasn't pressed last time
        // then set the return value to reflect this is a rising edge
        if (pressed && (shootLastPressedRising == false)) {
            returnValue = true;
        }

        // update the last pressed value to the currently pressed value
        shootLastPressedRising = pressed;

        // return the result of whether or not this was a rising edge
        return returnValue;
    }

    /*
     * Returns true if the deploy wedge button is pressed, false otherwise
     */
    public static boolean reversePressed() {
        return (m_joystick.getRawButton(BUTTON_REVERSE));
    }

    /*
     * This routine returns true if the reverse button is pressed and wasn't
     * pressed the prior time this routine was called.
     */
    public static boolean reverseRisingEdge() {
        // determine whether the button is currently pressed
        boolean pressed = reversePressed();

        // by default, set the return value to reflect this is not a rising edge
        boolean returnValue = false;

        // if the button is currently pressed, and it wasn't pressed last time
        // then set the return value to reflect this is a rising edge
        if (pressed && (reverseLastPressedRising == false)) {
            returnValue = true;
        }

        // update the last pressed value to the currently pressed value
        reverseLastPressedRising = pressed;

        // return the result of whether or not this was a rising edge
        return returnValue;
    }

    /*
     * Returns true if the deploy wedge button is pressed, false otherwise
     */
    public static boolean fullAutoPressed() {
        return (m_joystick.getRawButton(BUTTON_FULL_AUTO));
    }

    /*
     * Returns true if the deploy wedge button is pressed, false otherwise
     */
    public static boolean deployWedgePressed() {
        return (m_joystick.getRawButton(BUTTON_DEPLOY_WEDGE));
    }

    /*
     * Returns true if the retract wedge button is pressed, false otherwise
     */
    public static boolean retractWedgePressed() {
        return (m_joystick.getRawButton(BUTTON_RETRACT_WEDGE));
    }

    /*
     * Get the pitch (vertical / up - down) motion input from the stick
     */
    public static double getPitchValue() {
        // get the move value from the joystick
        double pitchValue = m_joystick.getRawAxis(AXIS_PITCH);

        if (pitchUpLimit.isClosed() && pitchValue > 0) {
            pitchValue = 0;
        } else if (pitchDownLimit.isClosed() && pitchValue < 0) {
            pitchValue = 0;
        }

        return pitchValue;
    }

    /*
     * Get the yaw (horizontal / left - right) motion input from the stick
     */
    public static double getYawValue() {
        // get the move value from the joystick
        return m_joystick.getRawAxis(AXIS_YAW);
    }
}
